package com.roberto.entities;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.utils.TimeUtils;
import com.roberto.main.Main;

public class Shot extends Entity {
    public boolean hasLeftField = false;
    private boolean players = false;
    private Sprite bullet;
    private Sprite[] frames;
    private int frameNumber;
    private float stateTime = 0;

    public Shot(float x, float y) {
        this.x = x;
        this.y = y;
        players = true;
        bullet = new Sprite(Main.megaTexture, 79, 58, 11, 35);
        bullet.setSize(4, 17);
        bullet.setPosition(x, y);
    }

    public Shot(float x, float y, int invaderId) {
        this.x = x;
        this.y = y;
        loadSprite(invaderId);
        bullet.setPosition(x, y);
    }

    private void loadSprite(final int invaderId) {
        int index = 56;
        int xx = TimeUtils.millis() % 2 == 1 ? 171 : 192;
        frames = new Sprite[4];
        for (int i = 0; i < frames.length; i++) {
            frames[i] = new Sprite(Main.megaTexture, index, xx, 12, 20);
            index += 13;
        }
        bullet = frames[0];
    }

    public void update(float delta) {
        if (players) {
            y += 500 * Gdx.graphics.getDeltaTime();
        } else {
            y -= 400 * Gdx.graphics.getDeltaTime();
            stateTime += Gdx.graphics.getDeltaTime();
            frameNumber = (int) (stateTime / 0.001f) % frames.length;
            bullet = frames[frameNumber];
        }

        if (y <= 30 || y >= Main.HEIGHT) {
            hasLeftField = true;
        }
        bullet.setPosition(x, y);
    }

    public boolean isPlayers() {
        return players;
    }

    public Sprite getSprite() {
        return bullet;

    }

    public final Rectangle getBounds() {
        return bullet.getBoundingRectangle();
    }

}
